package com.itlong.cloud.intercept;

import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.property.PropertyUserCacheVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.ecode.AppErrorCodeEnum;
import com.itlong.cloud.project.operate.IOperateProjectServiceClient;
import com.itlong.cloud.user.propertyapp.IPropertyAppUserInfoServiceClient;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <desc>
 *      物业app token拦截器
 * </desc>
 *
 * @createDate2019/12/24
 **/
@Aspect
@Component
public class PropertyAppLoginIntercept {

    private static Logger logger = LoggerFactory.getLogger(PropertyAppLoginIntercept.class);

    @Value("${"+ PlatformConstants.PROPERTY_APP_USER_TOKEN_DONTINTERCEPT +"}")
    String dontIntercept;

    @Autowired
    private IPropertyAppUserInfoServiceClient iPropertyAppUserInfoServiceClient;

    @Autowired
    private IOperateProjectServiceClient iOperateProjectServiceClient;

    @Pointcut("execution(* com.itlong.cloud.controller..*.*(..))")
    public void executeService(){}

    /**
     * <desc>
     *      token校验拦截器
     * </desc>
     *
     * @param joinPoint
     * @return
     * @Author Zhu.yj
     * @createDate  2019/12/24
     */
    @Around("executeService()")
    public Object loginIntercept(ProceedingJoinPoint joinPoint) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        //取出拦截的方法名
        String methodName = joinPoint.getSignature().getName();
        String[] interceptMethods = StringUtils.split(dontIntercept);
        //logger.info("【请求路由】："+request.getRequestURI());
        //跳过不拦截的方法
        for (String interceptMethod : interceptMethods) {
            if (interceptMethod.equals(methodName)) {
                return joinPoint.proceed();
            }
        }
        String token = request.getParameter("token");
        if (token == null) {
            token = request.getAttribute("token") == null ? null : request.getAttribute("token").toString();
        }
        String language = request.getParameter("language");
        if (language == null){
            language = request.getAttribute("language") == null ? null : request.getAttribute("language").toString();
        }
        if (StringUtils.isNotEmpty(token)){
            //根据token从缓存中取出user信息
            PropertyUserCacheVO propertyUserCacheVO =iPropertyAppUserInfoServiceClient.retrieve(token);
            if (propertyUserCacheVO == null){
                return new MessageVO(AppErrorCodeEnum.USER_TOKEN_ERROR.getErrorCode());
            }else {
                //判断账号是否被禁用
                if (propertyUserCacheVO.getStatus() ==2){
                    return new MessageVO(AppErrorCodeEnum.USER_ACCOUNT_REFUSE.getErrorCode());
                }else if (iOperateProjectServiceClient.getProjectStatus(propertyUserCacheVO.getProjectId()) == 2){
                    return new MessageVO(AppErrorCodeEnum.PAD_PROJECT_FORBIDDEN.getErrorCode());
                }
                return joinPoint.proceed();
            }
        }else {
            return new MessageVO(AppErrorCodeEnum.USER_TOKEN_ERROR.getErrorCode());
        }
    }
}
